package main.java.com.async.controller;

import java.io.File;
import java.io.IOException;

import com.alibaba.fastjson.JSONObject;
import main.java.com.async.app.MainDialog;
import main.java.com.async.middleware.StageManager;
import main.java.com.async.tools.CustomizeUtil;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class SettingDialogController {

	private static String _config_file_path = StageManager._var.get("config_file_path");
	private static JSONObject _json_Object;

	// ---------------------------------------------------
	// 控件的id 在此处申明
	// ---------------------------------------------------
	@FXML
	private TextField textfieldCreoLibPath; //Common Files\x86e_win64\lib路径的输入框
	@FXML
	private TextField textfieldCommMsgPath; //pro_msg_comm.exe 路径的输入框
	@FXML
	private TextField textfieldProDirPath; //pro_directory 路径的输入框
	@FXML
	private Button btnCreoPathBrowse; //parametric.exe 路径浏览按钮
	@FXML
	private Button btnMsgCommPathBrowse; //pro_msg_comm.exe 路径浏览按钮
	@FXML
	private Button btnProDirPathBrowse; //pro_directory 路径浏览按钮
	@FXML
	private Button btnLibBrowse; //模型库的路径浏览按钮
	@FXML
	private Button btnCancel; //取消设定按钮
	@FXML
	private Button btnConfirm; //确认保存设定按钮
	// ---------------------------------------------------


	/**
	 * 当前窗口初始化函数
	 * @throws IOException
	 */
	@FXML
	private void initialize() throws IOException {
		String config = CustomizeUtil.readFileToString(_config_file_path);
//		config加载为空或失败就退出程序
		if (config == null) {
			return;
		}
		_json_Object = JSONObject.parseObject(config);
		textfieldCreoLibPath.setText(_json_Object.getString("CreoLibPath"));
		textfieldCommMsgPath.setText(_json_Object.getString("ProCommMsg"));
		textfieldProDirPath.setText(_json_Object.getString("ProDirectory"));
	}

	/**
	 * 选择Parametric.exe的路径按钮
	 */
	@FXML
	private void onClickActionBtnCreoLibPathBrowse() {
		String chooserTitle = "选择lib文件夹";
		setFileorDirPath(null, chooserTitle, textfieldCreoLibPath);
	}

	/**
	 * 选择Pro_Comm_Msg.exe的路径按钮
	 */
	@FXML
	private void onClickActionBtnCommMsgPathBrowse() {
		ExtensionFilter extensionFilter = new ExtensionFilter("Pro_Comm_Msg (*.exe)", "*.exe");
		String chooserTitle = "选择Pro_Msg_Comm.exe文件";
		setFileorDirPath(extensionFilter, chooserTitle, textfieldCommMsgPath);
	}

	/**
	 * 选择Pro_Directory的路径按钮
	 */
	@FXML
	private void onClickActionBtnProDirPathBrowse() {
		String chooserTitle = "选择Pro_Directory文件夹路径";
		setFileorDirPath(null, chooserTitle, textfieldProDirPath);
	}

	/**
	 * 取消按钮
	 */
	@FXML
	private void onClickActionBtnCancel() {
		MainDialog mainDialog = new MainDialog();
		mainDialog.closeSettingDialog();
	}

	/**
	 * 确定保存按钮
	 * @throws IOException
	 */
	@FXML
	private void onClickActionBtnConfirm() throws IOException{
		MainDialog mainDialog = new MainDialog();
		CustomizeUtil customizeUtil = new CustomizeUtil();
		String config = CustomizeUtil.readFileToString(_config_file_path);
//		config加载为空或失败就退出程序
		if (config == null) {
			return;
		}
//		更新数据
		_json_Object = JSONObject.parseObject(config);
		_json_Object.put("CreoLibPath", textfieldCreoLibPath.getText());
		_json_Object.put("ProCommMsg", textfieldCommMsgPath.getText());
		_json_Object.put("ProDirectory", textfieldProDirPath.getText());
//		重新输出至文件
		customizeUtil.msgRecordToFile(_json_Object.toJSONString(), _config_file_path);
		mainDialog.closeSettingDialog();
	}

	// ---------------------------------------------------
	// 自定义函数
	// ---------------------------------------------------

	/**
	 * 设定文件或文件夹路径的辅助函数
	 * @param extensionFilter 扩展名过滤器对象,若为null,则默认选择文件夹路径
	 * @param chooserTitle 选择对话框的标题
	 * @param textField TextField控件对象
	 */
	private void setFileorDirPath(ExtensionFilter extensionFilter, String chooserTitle, TextField textField) {
		Stage browseStage = new Stage();
		if(extensionFilter != null) {
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle(chooserTitle);
			fileChooser.getExtensionFilters().add(extensionFilter);
			File file = fileChooser.showOpenDialog(browseStage);
			if(file != null) {
				textField.setText(file.getAbsolutePath());
			}
		}
		else {
			DirectoryChooser dirChooser = new DirectoryChooser();
			dirChooser.setTitle(chooserTitle);
			File fileDir = dirChooser.showDialog(browseStage);
			if(fileDir != null) {
				textField.setText(fileDir.getPath());
			}
		}

	}

}

